import 'package:flutter/material.dart';
import 'package:shop/page/category_page.dart';
import 'package:shop/router/router.dart';

import 'package:shop/page/home_page.dart';
import 'package:shop/page/cart_page.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateRoute: (settings) {
        return Routes.setRouts(settings);
      },
    );
  }
}

class TabbarPage extends StatefulWidget {
  const TabbarPage({super.key});

  @override
  State<TabbarPage> createState() => TabbarPageState();
}

class TabbarPageState extends State<TabbarPage> {
  final pageList = [
    HomePage(),
    CategoryPage(),
    CartPage(
      key: cartPagekey,
    )
  ];

  final List<BottomNavigationBarItem> items = [
    BottomNavigationBarItem(
        icon: Image.asset(
          'assets/images/home.png',
          width: 30,
          height: 30,
        ),
        activeIcon: Image.asset(
          'assets/images/home_active.png',
          width: 30,
          height: 30,
        ),
        label: '首页'),
    BottomNavigationBarItem(
        icon: Image.asset(
          'assets/images/cate.png',
          width: 30,
          height: 30,
        ),
        activeIcon: Image.asset(
          'assets/images/cate_active.png',
          width: 30,
          height: 30,
        ),
        label: '分类'),
    BottomNavigationBarItem(
        icon: Image.asset(
          'assets/images/cart.png',
          width: 30,
          height: 30,
        ),
        activeIcon: Image.asset(
          'assets/images/cart_active.png',
          width: 30,
          height: 30,
        ),
        label: '购物车')
  ];

  var currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        items: items,
        onTap: (index) {
          switchTab(index);
        },
      ),
      body: IndexedStack(
        index: currentIndex,
        children: pageList,
      ),
    );
  }

  void switchTab(int index) {
    setState(() {
      currentIndex = index;
    });

    // final cpage = pageList[index];
    // if (cpage is CartPage) {
    //   final sized = cartPagekey.currentState as CartPageState;
    //   sized.getShopCart();
    // }
  }
}
